package com.ericdm.database;

import com.ericdm.mydoubanmovie.MainApplication;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseOperator {
	ContentValues contentValues = new ContentValues();
	public static final String DATABASE_NAME = "USERNAME_AND_PASSWORD";
	public static final String TABLE_NAME = "TABLE_USERNAME_PASSWORD";

	public void insertData(String dataUserName, String dataPassword) {
		DatabaseHelper databaseHelper = new DatabaseHelper(MainApplication.getContext(), DATABASE_NAME);
		SQLiteDatabase sqLiteDatabase = databaseHelper.getWritableDatabase();
		contentValues.clear();
		contentValues.put("USERNAME", dataUserName);
		contentValues.put("PASSWORD", dataPassword);
		sqLiteDatabase.insert(TABLE_NAME, null, contentValues);
		sqLiteDatabase.close();
	}
	
	public void deleteRecord(String userName) {
		DatabaseHelper databaseHelper = new DatabaseHelper(MainApplication.getContext(), DATABASE_NAME);
		SQLiteDatabase sqLiteDatabase = databaseHelper.getWritableDatabase();
		sqLiteDatabase.delete(TABLE_NAME, "USERNAME=?", new String[] {userName});
		sqLiteDatabase.close();
	}
	
	public String queryPassword(String userName) {
		DatabaseHelper databaseHelper = new DatabaseHelper(MainApplication.getContext(), DATABASE_NAME);
		SQLiteDatabase sqLiteDatabase = databaseHelper.getReadableDatabase();
		Cursor cursor = sqLiteDatabase.query(TABLE_NAME, new String[] { "PASSWORD"}, "USERNAME=?", new String[] {userName}, null, null, null);
		String passWord = "";
		while (cursor.moveToNext()){
			passWord = cursor.getString(cursor.getColumnIndex("PASSWORD")); 
		}
		sqLiteDatabase.close();
		return passWord;
	}
}
